//
//  DetailPersonalInfoVC.m
//  CreditHub
//
//  Created by chenglang on 2024/9/21.
//

#import "DetailPersonalInfoVC.h"
#import "InfoTextInputView.h"
#import "HomeRequest.h"
#import "BankPopupViewController.h"

@interface DetailPersonalInfoVC ()
@property (weak, nonatomic) IBOutlet UIView *contentView;
@property (weak, nonatomic) IBOutlet UIImageView *imageTitle;
@property (weak, nonatomic) IBOutlet UILabel *labelTitle;
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *layoutHeight;
@property (nonatomic , strong) NSMutableArray *arraySuchan;

@end

@implementation DetailPersonalInfoVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navBgImageView.hidden = NO;
    self.startTime = [BaseService getTimeStringSince1970];
    if(self.type == 1){
        self.pointType = @"6";
        self.ch_title = @"Personal information";
        self.imageTitle.image = [UIImage imageNamed:@"cer_header_personal"];
        self.labelTitle.text = @"Please fill in your information truthfully below";
    }else if (self.type == 2){
        self.pointType = @"7";
        self.ch_title = @"Job information";
        self.imageTitle.image = [UIImage imageNamed:@"cer_header_job"];
        self.labelTitle.text = @"Please fill in your information truthfully below";
    }else if (self.type == 3){
        self.pointType = @"9";
        self.ch_title = @"Bank information";
        self.imageTitle.image = [UIImage imageNamed:@"cer_header_bank"];
        self.labelTitle.text = @"Please fill in your information truthfully below";
    }

    [self requestDetail];
    
}
- (void)requestDetail{
    __weak typeof(self) weakSelf = self;
    [HomeRequest requestCerPersonalInfoId:self.detailId type:self.type success:^(NSURLSessionDataTask *task, id response) {
        if([response[@"ofexcitement"] integerValue] == 0){
            weakSelf.arraySuchan = [NSMutableArray arrayWithArray:response[@"pity"][@"suchan"]];
            for(int i = 0 ; i < weakSelf.arraySuchan.count;i++){
                NSDictionary *dicInfo = weakSelf.arraySuchan[i];
                InfoTextInputView *view = [[NSBundle mainBundle] loadNibNamed:@"InfoTextInputView" owner:self options:@{}].firstObject;
                view.tag = 2000+i;
                view.dicInfo = [NSMutableDictionary dictionaryWithDictionary:dicInfo];
                view.labelTitle.text = CHString(dicInfo[@"outside"]);
                view.textFileld.placeholder = CHString(dicInfo[@"sides"]);
                [weakSelf.contentView addSubview:view];
                [view mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.left.mas_equalTo(15);
                    make.right.mas_equalTo(-15);
                    make.height.offset(80);
                    make.top.mas_equalTo(140+i*90);
                }];
            }
            weakSelf.layoutHeight.constant = 140 +20+  weakSelf.arraySuchan.count*90;
            
        }
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
}

- (IBAction)clickSubmit:(id)sender {
    [self.view endEditing:YES];
   
    
    if(self.type == 3){
        
        NSString *account = @"";
        for(int i = 0 ; i < self.arraySuchan.count;i++){
            InfoTextInputView *view = [self.contentView viewWithTag:2000+i];
            NSDictionary *dicInfo = view.dicInfo;
            if([CHString(dicInfo[@"ofexcitement"]) isEqualToString:@"cardNo"]){
                account = CHString(dicInfo[@"spindly"]);
                break;
            }
        }
        
        __weak typeof(self) weakSelf = self;
        BankPopupViewController *vc = [[BankPopupViewController alloc] init];
        vc.modalPresentationStyle = UIModalPresentationOverFullScreen;
        vc.account = account;
        vc.okBlock = ^{
            [weakSelf requestSave];
        };
        [[UIView xhl_getTopViewController] presentViewController:vc animated:YES completion:^{
            
        }];
    }else{
        [self requestSave];
    }
}
- (void)requestSave{
    
    NSMutableDictionary *dicParams = [NSMutableDictionary dictionary];
    for(int i = 0 ; i < self.arraySuchan.count;i++){
        InfoTextInputView *view = [self.contentView viewWithTag:2000+i];
        NSDictionary *dicInfo = view.dicInfo;
            [dicParams setValue:CHString(dicInfo[@"spindly"]) forKey: CHString(dicInfo[@"ofexcitement"])];
    }
    [dicParams setValue:self.detailId forKey:@"lightat"];
    __weak typeof(self) weakSelf = self;
    xhl_hudLoading();
    [HomeRequest requestSaveCerPersonalInfoId:dicParams type:self.type success:^(NSURLSessionDataTask *task, id response) {
        if([response[@"ofexcitement"] integerValue] == 0){
            [weakSelf.navigationController popViewControllerAnimated:YES];
            weakSelf.endTime = [BaseService getTimeStringSince1970];
            [weakSelf requestUploadPointType];
        }
        xhl_showMessage(response[@"shared"]);
        
      
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        xhl_hudDismiss();
    }];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
